package com.rbac.controller;

import com.apis.user.UserApis;
import com.common.annotation.Authority;
import com.domain.dto.PageDTO;
import com.domain.dto.user.AddUserDTO;
import com.domain.dto.user.ModifyUserDTO;
import com.domain.dto.user.ModifyUserRoleDTO;
import com.domain.dto.user.UserDTO;
import com.domain.query.user.UserQuery;
import com.domain.vo.JsonVO;
import com.rbac.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "用户操作控制层")
@RestController
@RequestMapping("user")
public class UserController implements UserApis {
    @Autowired
    private IUserService userService;
    //查询用户列表
    @Override
    @GetMapping("/query-list")
    @Authority("system:user:list")
    @Operation(summary = "查询用户列表（条件+分页）")
    public JsonVO<PageDTO<UserDTO>> queryUserList(@Validated @ParameterObject UserQuery userQuery) {
        PageDTO<UserDTO> userPage = userService.queryUserList(userQuery);
        return JsonVO.success(userPage);
    }

    //查询具体用户
    @Override
    @GetMapping("/query-id/{userId}")
    @Authority("system:user:query")
    @Operation(summary = "查询用户数据（单条）")
    public JsonVO<UserDTO> queryUserByUserId(@PathVariable Integer userId){
        UserDTO userDTO = userService.queryUserByUserId(userId);
        return JsonVO.success(userDTO);
    }

    //新增用户
    @Override
    @GetMapping("/add")
    @Authority("system:user:add")
    @Operation(summary = "添加用户")
    public JsonVO<Integer> addUser(@Validated @ParameterObject AddUserDTO addUserDTO){
        Integer addedRow = userService.addUser(addUserDTO);
        return addedRow > 0 ? JsonVO.success(addedRow) : JsonVO.fail(0);
    }

    //修改用户
    @Override
    @PutMapping("/modify")
    @Authority("system:user:modify")
    @Operation(summary = "修改用户")
    public JsonVO<Integer> modifyUser(@Validated @ParameterObject ModifyUserDTO modifyUserDTO){
        Integer modifiedRow = userService.modifyUser(modifyUserDTO);
        return modifiedRow > 0 ? JsonVO.success(modifiedRow) : JsonVO.fail(0);
    }

    //删除用户
    @Override
    @DeleteMapping("/remove/{userIds}")
    @Authority("system:user:remove")
    @Operation(summary = "删除用户（支持批量删除）")
    public JsonVO<Integer> removeUser(@PathVariable List<Integer> userIds){
        Integer deletedRow = userService.removeUser(userIds);
        return deletedRow > 0 ? JsonVO.success(deletedRow) : JsonVO.fail(0);    }


    @Override
    @PostMapping("/modify/user-role")
    @Authority("system:role:modify")
    @Operation(summary = "修改用户角色定位（用户编号）")
    public JsonVO<Integer> modifyRoleUser(@Validated @ParameterObject ModifyUserRoleDTO modifyUserRoleDTO) {
        return JsonVO.success(userService.modifyRoleUser(modifyUserRoleDTO));
    }

    @Override
    @GetMapping("/query-role-name")
    @Authority("system:role:list")
    @Operation(summary = "查询角色名称列表（用户编号）")
    public JsonVO<List<String>> queryRoleNameListByUserId(Integer userId) {
        return JsonVO.success(userService.queryRoleNameListByUserId(userId));
    }


}
